<template>
	<view class="login">
		<!-- 顶部结构start -->
		<view class="nav">
			<view class="nav-left">
				<view class="nav-icon">
					<image src="../../static/images/home_active.png" mode="widthFix" class="img"></image>
				</view>
				<view class="nav-txet">
					Luckin Coffee
				</view>
			</view>
			<view class="nav-right">
				先逛一逛
			</view>
		</view>
		<!-- 顶部结构end -->
		<!-- 文本结构 start -->
		<view class="section">

			<view class="section-name">
				找回密码！
			</view>
			<view class="section-enname">
				Find password
			</view>
		</view>
		<!-- 文本结构 start -->
		<!-- 登录结构 start -->
		<view class="nav-login">
			<u-form :model="form" ref="uForms">
				<u-form-item label="邮箱" prop="email">
					<u-input v-model="form.email" type="text" placeholder="请输入邮箱" />
				</u-form-item>
				<u-form-item label="验证码" prop="code">
					<u-input v-model="form.code" type="text" placeholder="请输入验证码" />
					<button class="code" @click="time" :class="isFlag?'a':''">
						{{isFlag?times+"秒":'发送验证码'}}
					</button>
				</u-form-item>
			</u-form>
			<view class="goTo" @click="goTo">
				已有账号，立即登录
			</view>
		</view>
		<!-- 登录结构 end-->
		<button @click="submit" class="btn_1 common">下一步</button>

	</view>
</template>

<script>
	import request from "../../request/request.js"
	export default {
		data() {
			return {
				isFlag: false,
				show: false,
				tiemer: null,
				times: 11,
				num: 11,
				form: {
					email: '',
					code: ""
				},
				rules: {
					email: [{
						required: true,
						pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
						message: '请输入正确邮箱',

					}],
					code: [{
						required: true,
						pattern: /^\d{6}$/,
						message: '请输入正确验证码',

					}]
				}
			}
		},
		methods: {
			submit() {
				this.$refs.uForms.validate(valid => {
					if (valid) {
						this.getCode()
						
					} else {
						console.log('验证失败');
					}
				});
			},
			getCode(){
				request({
					url: "/checkValidCode",
					method: "POST",
					data: {
						validCode:this.form.code
					},
					success:(res)=>{
						console.log(res)
						if(res.data.code=="K001"){
							uni.showToast({
								title:res.data.msg,
								icon:"none",
								mask:true,
								duration:1000
							})
							setTimeout(()=>{
								uni.navigateTo({
									url: "../sigin/sigin"
								})
							},1000)
							
						}
						
						
					}
				})
			},
			getEmail(){
				request({
					url: "/emailValidCode",
					method: "POST",
					data: {
						email:this.form.email
					},
					success:(res)=>{
						console.log(res)
						if(res.code==200){
							uni.showToast({
								title:res.data.msg,
								icon:"none",
								mask:true,
								duration:1000
							})
						}
						
						
					}
				})
			},

			goTo() {

				uni.navigateTo({
					url: "../login/login"
				})
			},
			time() {
				console.log(this.num)
				// 显示倒计时
				this.isFlag = !this.isFlag
				if (this.isFlag) {
					this.getEmail()
					this.tiemer = setInterval(() => {
						// 倒计时 时长
						this.num--
						// 判断 是否小于 补齐十位
						if (this.num < 10) {
							this.times = "0" + this.num
						} else {
							this.times = this.num
						}
						//倒计时 结束   
						if (this.num == 0) {
							//清空 定时器  
							clearInterval(this.tiemer)
							this.tiemer = null
							// 初始化 倒计时 时长
							this.num = 11
							this.times=11
							// 关闭 倒计时
							this.isFlag = false
						}
					}, 1000)
				}

			}
		},
		onReady() {
			this.$refs.uForms.setRules(this.rules);
		}
	}
</script>

<style lang="less" scoped>
	.login {
		//禁止点击
		.a {
			pointer-events: none;
			background-color: #999 !important;
		}

		.nav {
			height: 120rpx;
			padding: 0 30rpx;
			padding-bottom: 20rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			align-items: center;
		
			.nav-left {
				flex: 7.5;
				display: flex;
				align-items: center;
		
				.nav-icon {
					width: 75rpx;
					height: 75rpx;
		
					.img {
						width: 100%;
					}
				}
		
				.nav-txet {
					color: #646566;
					font-size: 38rpx;
					font-weight: bold;
					margin-left: 30rpx;
				}
			}
		
			.nav-right {
				color: #0c34ba;
				font-size: 34rpx;
				font-weight: bold;
				flex: 2.5;
		
			}
		}
		
		.section {
			margin: 0 30rpx;
			margin-top: 180rpx;

			.section-name {
				font-size: 80rpx;
				font-weight: bold;
			}

			.section-enname {
				margin: 50rpx 0;
				margin-bottom: 80rpx;
				font-size: 40rpx;
			}
		}

		.nav-login {
			.code {
				width: 230rpx;
				height: 70rpx;
				color: #fff;
				font-size: 20rpx;
				text-align: center;
				line-height: 70rpx;
				background-color: #0c34ba;
				float: right;
			}

			.goTo {
				margin: 50rpx 30rpx;
				float: right;
			}
		}

		.common {
			height: 100rpx;
			font-size: 34rpx;
			border-radius: 80rpx;
			margin: 50rpx 30rpx;
			line-height: 100rpx;
			text-align: center;
		}

		.btn_1 {
			color: #fff;
			background-color: #0c34ba;
			margin-top: 150rpx;
		}

		.btn_2 {
			border: 2rpx solid #ccc;
		}

		::v-deep .u-border-bottom {
			border-bottom: 2rpx solid #ccc;
			margin: 0 50rpx;
		}

		::v-deep .u-form-item--left__content__label {
			width: 100rpx;
		}

		::v-deep .u-form-item--right__content__slot {
			margin-left: 30rpx;
		}

		::v-deep .u-form-item__message {
			display: none;
		}
	}
</style>
